using System.Collections.Generic;
using System.Text.Json.Serialization;
using JlPay.Sdk.Models;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Models.Openmerch.Query
{
    /// <summary>
    /// 商户渠道信息查询响应
    /// </summary>
    public class MerchChannelInfoQueryResponse : StandardResponse, IComplexFieldHandler
    {
        /// <summary>
        /// 商户号
        /// </summary>
        [JsonPropertyName("merch_no")]
        public string? MerchNo { get; set; }

        /// <summary>
        /// 商户名称
        /// </summary>
        [JsonPropertyName("merch_name")]
        public string? MerchName { get; set; }

        /// <summary>
        /// 渠道信息列表
        /// </summary>
        [JsonPropertyName("channel_info")]
        public List<ChannelInfo>? ChannelInfo { get; set; }

        /// <summary>
        /// 处理复杂字段
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="fieldValue">字段值</param>
        public void HandleComplexField(string fieldName, object fieldValue)
        {
            if (fieldName == "channel_info")
            {
                if (fieldValue is System.Text.Json.JsonElement jsonElement)
                {
                    if (jsonElement.ValueKind == System.Text.Json.JsonValueKind.Array)
                    {
                        ChannelInfo = new List<ChannelInfo>();
                        foreach (var item in jsonElement.EnumerateArray())
                        {
                            if (item.ValueKind == System.Text.Json.JsonValueKind.Object)
                            {
                                var channelInfo = System.Text.Json.JsonSerializer.Deserialize<ChannelInfo>(item.GetRawText(), new System.Text.Json.JsonSerializerOptions 
                                { 
                                    PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.SnakeCaseLower 
                                });
                                if (channelInfo != null)
                                {
                                    ChannelInfo.Add(channelInfo);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 渠道信息
    /// </summary>
    public class ChannelInfo
    {
        /// <summary>
        /// 渠道ID
        /// </summary>
        [JsonPropertyName("channel_id")]
        public string? ChannelId { get; set; }

        /// <summary>
        /// 渠道商户号
        /// </summary>
        [JsonPropertyName("channel_merch_no")]
        public string? ChannelMerchNo { get; set; }

        /// <summary>
        /// 渠道商户名称
        /// </summary>
        [JsonPropertyName("channel_merch_name")]
        public string? ChannelMerchName { get; set; }

        /// <summary>
        /// 上报状态
        /// </summary>
        [JsonPropertyName("report_status")]
        public string? ReportStatus { get; set; }

        /// <summary>
        /// 渠道商户简称
        /// </summary>
        [JsonPropertyName("channel_merch_shortname")]
        public string? ChannelMerchShortname { get; set; }

        /// <summary>
        /// 上报时间
        /// </summary>
        [JsonPropertyName("report_time")]
        public string? ReportTime { get; set; }

        /// <summary>
        /// 失败原因
        /// </summary>
        [JsonPropertyName("fail_reason")]
        public string? FailReason { get; set; }

        /// <summary>
        /// 费率类型
        /// </summary>
        [JsonPropertyName("fee_type")]
        public string? FeeType { get; set; }
    }
} 